package com.lubase.metadata.invoke;

import com.alibaba.fastjson.JSON;
import com.lubase.core.extend.IInvokeMethod;
import com.lubase.metadata.model.EMapType;
import com.lubase.metadata.model.MapLayoutModel;
import com.lubase.metadata.service.RelationMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class setRelationMapLayout implements IInvokeMethod {
    @Autowired
    RelationMapService relationMapService;

    @Override
    public Object exe(HashMap<String, String> mapParam) throws Exception {
        String businessId = checkAndGetParam("businessId", mapParam);
        EMapType mapType = getMapType(checkAndGetParam("mapType", mapParam));
        if (mapType == null) {
            return "mapType参数错误";
        }
        String relationMapListStr = checkAndGetParam("relationMapListStr", mapParam);
        List<MapLayoutModel> relationMapList = JSON.parseArray(relationMapListStr, MapLayoutModel.class);

        return relationMapService.setTableRelationMap(businessId, mapType, relationMapList);
    }

    private EMapType getMapType(String mapType) {
        switch (mapType) {
            case "1":
                return EMapType.Database;
            case "2":
                return EMapType.Table;
            case "3":
                return EMapType.BizScene;
        }
        return null;
    }

    @Override
    public String getDescription() {
        return "元数据库管理：更新数据库ER图位置";
    }

    @Override
    public String getId() {
        return "1473050059787472896";
    }
}
